package com.example.music;

import java.io.File;
import java.util.List;
import java.util.Map;

import com.example.adapter.MusicControl;
import com.example.adapter.MusicListAdapter;
import com.xiang.data.Contant;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

	private ListView list;
	private List<String> fileList_test;
	public static MusicListAdapter adapter;
	public static List<Map<String, Object>> fileList;
	
	private RelativeLayout music_control;

	public static int musicNum;// 当前播放音乐列表的音乐位置
	public static String filePath; // 音乐路径

	public static playHandler handler;



	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		if (handler == null) {
			handler = new playHandler();
		}

		music_control = (RelativeLayout) findViewById(R.id.rl_music_control);
		music_control.setOnClickListener(this);
		list = (ListView) findViewById(R.id.main_listview);

		File rootPath = Manager.getSDcardROOT();
		File musicFile = new File(rootPath.getPath() + "//" + "musictest");
		fileList = Manager.getData(musicFile);

		showMusicList();

		list.setOnItemClickListener(new listItemsListener());
		

	}

	// 显示本地音乐列表
	private void showMusicList() {
		if (adapter == null) {
			adapter = new MusicListAdapter(this, fileList);
			list.setAdapter(adapter);
		} else {
			adapter.resetData(fileList);
			adapter.notifyDataSetChanged();
		}

	}

	// 点击音乐列表
	class listItemsListener implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
				if (musicNum == arg2) {
					playMusic(MusicControl.MUSIC_CONTROL);
				} else {
					musicNum = arg2;
					filePath = (String) fileList.get(musicNum).get("filepath");
					playMusic(MusicControl.MUSIC_LOADING);
				}
		}
	}

	// 播放音乐
	private void playMusic(int control) {
		Intent intent = new Intent(MainActivity.this, MusicControl.class);
		intent.putExtra("control", control);
		startService(intent);
	}

	@Override
	protected void onDestroy() {
		Intent intent = new Intent();
		intent.setClass(MainActivity.this, MusicControl.class);
		stopService(intent);// 停止Service
		adapter = null;
		super.onDestroy();
	}

	public static MusicListAdapter getAdapter() {
		return adapter;
	}

	public class playHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);

			if (msg.what == Contant.MUSIC_LAST) {// 上一曲
				if (musicNum <= 0) {
					musicNum = fileList.size() - 1;
					filePath = (String) fileList.get(musicNum).get("filepath");

				} else {
					musicNum--;
					filePath = (String) fileList.get(musicNum).get("filepath");
				}
				playMusic(MusicControl.MUSIC_LOADING);
			} else if (msg.what == Contant.MUSIC_NEXT) {// 下一曲
				if (musicNum >= fileList.size() - 1) {
					musicNum = 0;
					filePath = (String) fileList.get(musicNum).get("filepath");
				} else {
					musicNum++;
					filePath = (String) fileList.get(musicNum).get("filepath");
				}
				playMusic(MusicControl.MUSIC_LOADING);
			} else if (msg.what == Contant.MUSIC_PLAY) {// 播放、暂停
				playMusic(MusicControl.MUSIC_CONTROL);
			}else if(msg.what == Contant.MUSICPATH_NULL){
				Toast.makeText(MainActivity.this, "找不音乐文件", Toast.LENGTH_SHORT).show();
			}
			
		}
	}

	@Override
	public void onClick(View v) {
		int vid = v.getId();
		switch (vid) {
		case R.id.rl_music_control://打开播放界面
			Intent intent = new Intent(MainActivity.this,
					MusicPlayActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}
		
	}

}
